package com.fh.service.transactionpair;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.fh.apientity.Kline;
import com.fh.apientity.KlineResponse;
import com.fh.apientity.Merged;
import com.fh.apientity.MergedResponse;
import com.fh.dao.DaoSupport;
import com.fh.entity.Page;
import com.fh.service.system.redis.RedisService;
import com.fh.util.ApiClientUtil;
import com.fh.util.AppUtil;
import com.fh.util.Const;
import com.fh.util.JsonStringUtil;
import com.fh.util.PageData;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


@Service("transactionpairService")
public class TransactionPairService {
	@Resource(name="redisService")
	private RedisService redisService;
	
	@Resource(name = "daoSupport")
	private DaoSupport dao;
	
	/*
	* 新增
	*/
	public void save(PageData pd)throws Exception{
		dao.save("TransactionPairMapper.save", pd);
	}
	
	/*
	* 删除
	*/
	public void delete(PageData pd)throws Exception{
		dao.delete("TransactionPairMapper.delete", pd);
	}
	
	/*
	* 修改
	*/
	public void edit(PageData pd)throws Exception{
		dao.update("TransactionPairMapper.edit", pd);
	}
	
	/*
	*列表
	*/
	public List<PageData> list(Page page)throws Exception{
		
		return (List<PageData>)dao.findForList("TransactionPairMapper.datalistPage", page);
	}
	
	/*
	*列表(全部)
	*/
	public List<PageData> listAll(PageData pd)throws Exception{
		return (List<PageData>)dao.findForList("TransactionPairMapper.listAll", pd);
	}
	
	/*
	* 通过id获取数据
	*/
	public PageData findById(PageData pd)throws Exception{
		return (PageData)dao.findForObject("TransactionPairMapper.findById", pd);
	}
	
	/*
	* 批量删除
	*/
	public void deleteAll(String[] ArrayDATA_IDS)throws Exception{
		dao.delete("TransactionPairMapper.deleteAll", ArrayDATA_IDS);
	}
	
	/**
	 * 根据币种交易对来取火币接口聚合行情数据
	 * @return
	 * @throws Exception
	 */
	public Object getmerged()throws Exception{
		
		Map<String,Object> map = new HashMap<String,Object>();
		if(redisService.exists("currenJson")){
			List<PageData> currenname=redisService.getList("currenJson",PageData.class);
			for(int i = 0;i<currenname.size();i++){
				String mergedResponse= ApiClientUtil.merged(currenname.get(i).getString("CURRENCY_USERNAME"));//取火币聚合行情数据
				map.put(currenname.get(i).toString(), mergedResponse);
				
			}
		}
		return  map;
	}
	
	
	/**
	 * 根据币种交易对来取火币接口聚合行情数据和k线图
	 * @return
	 * @throws Exception
	 */
	public Object getklineMerged(JSONObject menssageSocket)throws Exception{
		
		Map<String,Object> map = new HashMap<String,Object>();
		String mergedResponse= ApiClientUtil.merged(menssageSocket.getString("symbol"));//取火币聚合行情数据
		KlineResponse klineResponse=ApiClientUtil.kline(menssageSocket.getString("symbol"),menssageSocket.getString("period"),Const.GITHUB);//取火币k情数据	
//		System.out.println(mergedResponse.data);
		map.put("merged", mergedResponse);
		List<Kline> klineList = (List<Kline>)klineResponse.data;
		List<Kline> klineLists = new ArrayList<Kline>();
		int klinesize = klineList.size();
		for(int i = 0;i<klinesize;i++){
			Kline kline =new Kline();
			kline.setAmount(klineList.get(i).getAmount());
			kline.setClose(klineList.get(i).getClose()); 
			kline.setCount(klineList.get(i).getCount()); 
			kline.setHigh(klineList.get(i).getHigh()); 
			kline.setId(klineList.get(i).getId()); 
			kline.setLow(klineList.get(i).getLow()); 
			kline.setOpen(klineList.get(i).getOpen()); 
			kline.setVol(klineList.get(i).getVol()); 
			klineLists.add(kline);
		}
		map.put("kline", klineLists);
		return map;
	}
	
	
	
}

